package com.csxy.chy.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.kms.model.v20160120.UpdateSecretResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.csxy.chy.exception.MyException;
import com.csxy.chy.service.VodService;
import com.csxy.chy.service.util.ConstantVodUtils;
import com.csxy.chy.service.util.InitObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadAliyunVideo(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        //title:上传之后显示名称
        String title = fileName.substring(0,fileName.lastIndexOf("."));
        //inputStream:上传文件的输入流
        try{
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESSKEY_ID
                    , ConstantVodUtils.ACCESSKEY_SECRET
                    , title, fileName
                    , inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId()+"\n");//请求视频点播服务的请求ID
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
                System.out.print("VideoId=" + getPlayUrl(videoId));
            } else {//如果设置回调url无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空,此时请求ID为空
                videoId = response.getVideoId();
                System.out.println("VideoId = " + getPlayUrl(videoId));
            }
            return videoId;
        }catch (IOException e){
            throw new MyException(20001,"文件上传失败");
        }
    }
    //根据ID删除
    @Override
    public void removeAliyunVideoById(String id) {
        try {
            // 初始化点播客户端对象
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID, ConstantVodUtils.ACCESSKEY_SECRET);
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置要删除的视频id
            request.setVideoIds(id);
            // 调用方法实现删除
            DeleteVideoResponse response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new MyException(20001, "视频删除失败");
        }
    }

    //批量删除
    @Override
    public void removeMoreVideo(List<String> videoIdList) {
        // 将集合转换为1,2,3格式
        String str = StringUtils.join(videoIdList.toArray(), ",");
        try {
            // 初始化点播客户端对象
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID, ConstantVodUtils.ACCESSKEY_SECRET);
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置要删除的视频id
            request.setVideoIds(str);
            // 调用方法实现删除
            DeleteVideoResponse response = client.getAcsResponse(request);
        } catch (Exception e) {
            throw new MyException(20001, "批量视频删除失败");
        }
    }

    public String getPlayUrl(String videoId) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            GetPlayInfoResponse response = client.getAcsResponse(request);
            return response.getPlayInfoList().get(0).getPlayURL();
        } catch (ClientException e) {
            throw new MyException(20001, "获取播放地址失败");
        }
    }
}
